import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;


public class ReadIOPairs 
{
	public ArrayList<Tuple> readIOPairsFromFile(ArrayList<Tuple> IOPairs, String filename)
	{
		BufferedReader in;
		
		try 
		{
			in = new BufferedReader(new FileReader(filename));
			
			Tuple tempTuple = new Tuple();
			ArrayList<Integer> inputList = new ArrayList<Integer>();
			ArrayList<Integer> outputList = new ArrayList<Integer>();
			
			String tempValue = "";
			String row = null;
			
			while((row = in.readLine()) != null)
			{
				boolean inputListDone = false;
				if(row.startsWith("["))
				{
					for(int i=0;i<row.length();i++)
					{
						if(row.charAt(i) == '1' || 
								row.charAt(i) == '2' || 
								row.charAt(i) == '3' ||
								row.charAt(i) == '4' ||
								row.charAt(i) == '5' ||
								row.charAt(i) == '6' ||
								row.charAt(i) == '7' ||
								row.charAt(i) == '8' ||
								row.charAt(i) == '9' ||
								row.charAt(i) == '0' ||
								row.charAt(i) == '-')

							tempValue = tempValue + row.charAt(i);
						
						else if(row.charAt(i) == ',' || row.charAt(i) == ']')
						{
							if(!inputListDone)
								inputList.add(Integer.parseInt(tempValue));
							else
								outputList.add(Integer.parseInt(tempValue));
							
							tempValue = "";
						}
						
						else if(row.charAt(i) == '=')
							inputListDone = true;
					}
					inputListDone = false;
					tempTuple.setInputList(inputList);
					tempTuple.setOutputList(outputList);
					IOPairs.add(tempTuple);
					tempTuple = new Tuple();
					inputList = new ArrayList<Integer>();
					outputList = new ArrayList<Integer>();
				}
			}
		} 
		catch (FileNotFoundException e) 
		{
			System.out.println("File '" + filename + "' not found.");
			System.exit(0);
		} 
		catch (IOException e) 
		{
			System.out.println("IOException on readline()");
			System.exit(0);
		}
		
		return IOPairs;
	}
}
